package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Menu;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.model.vo.MenuVO;
import com.symone.webcms.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *   @description : 菜单管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-01-02
 */
@RestController
@Api(value="/menu", description="菜单管理")
@RequestMapping("/api/menu")
public class MenuController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    public MenuService menuService;

    /**
     * @description : 获取菜单分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(Integer pageSize, Integer pageNum) {
        Wrapper<Menu> ew = new EntityWrapper<Menu>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<Menu> page = new Page<Menu>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        menuService.findPage(page, ew);

        return ResultMessage.success("获取菜单的分页列表",page);
    }

    /**
     * @description : 获取所有菜单记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultMessage list() {
        List<Menu> menus = menuService.selectList(new EntityWrapper<Menu>().eq("deleted",0).orderBy("sorting"));
        List<Menu> result = new ArrayList<>();
        for(Menu m : menus){
            if(m.getPid() == 0){
                m.setSorting(m.getSorting() * 10000);
                result.add(m);
                for(Menu sub : menus){
                    if(sub.getPid() ==  m.getId()){
                        sub.setSorting(m.getSorting() + sub.getSorting());
                        result.add(sub);
                    }
                }
            }else{


            }
        }
        Collections.sort(result);
        return ResultMessage.success("获取所有菜单信息",result);
    }

    /**
     * @description : 通过id获取菜单
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取菜单信息成功!",menuService.selectById(id));
    }

    /**
     * @description : 添加菜单
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(MenuVO menu){
        menuService.insert(menu);
        return ResultMessage.success("添加菜单成功!");
    }

    /**
     * @description : 通过id逻辑删除菜单
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(Long id){
        if(menuService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑菜单删除成功！");
        } else {
            return ResultMessage.fail("逻辑菜单删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除菜单
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(menuService.deleteById(id)) {
            return ResultMessage.success("物理删除菜单成功！");
        } else {
            return ResultMessage.fail("物理删除菜单失败！");
        }
    }

    /**
     * @description : 更新菜单信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
     */
    @RequiresPermissions("dev:menu:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage update(MenuVO menu) {
        if(menuService.update(menu)) {
            return ResultMessage.success("菜单信息更新成功!");
        }else{
            return ResultMessage.fail("菜单信息更新失败!");
        }
    }

    /**
    * @description : 清除菜单记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-02
    */
    @RequiresPermissions("dev:menu:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = menuService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有菜单记录被清除！");
        }
    }



}
